import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'
import type { RouteRecordRaw } from "vue-router";
import Register from '@/views/Register.vue'
import businessInfo from '@/views/businessInfo.vue'
import businessList from '@/views/businessList.vue'
import order from '@/views/order.vue'
import payment from '@/views/payment.vue'
import orderList from '@/views/orderList.vue'
import my from '@/views/my.vue'
// 定义路由记录
const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/Home',
        meta: { requiresAuth: false }
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '用户登录 - 饿了么',
            requiresAuth: false
        }
    },
    {
        path: '/Home',
        name: 'Home',
        component: Home,
        meta: {
            title: '首页 - 饿了么',
            requiresAuth: false
        }
    },
    {
        path: '/register',
        name: 'register',
        component: Register,
        meta: {
            title: '用户注册 - 饿了么',
            requiresAuth: false
        }
    },
    {
        path:'/businessInfo',
        name:'businessInfo',
        component: businessInfo,
        meta:{
            title:'商家信息 - 饿了么',
            requiresAuth:false
        }
    },
    {
        path:'/businessList',
        name:'BusinessList',
        component: businessList,
        meta:{
            title:'商家列表 - 饿了么',
            requiresAuth:false
        }
    },

    {
        path:'/order',
        name:'order',
        component: order,
        meta:{
            title:'订单 - 饿了么',
            requiresAuth:false
        }
    },
    {

        path:'/payment',
        name:'payment',
        component: payment,
        meta:{
            title:'支付 - 饿了么',
            requiresAuth:false
        }
    },
    {
        path:'/orderList',
        name:'orderList',
        component: orderList,
        meta:{
            title:'订单列表 - 饿了么',
            requiresAuth:false
        }
    },
    {
        path:'/my',
        name:'my',
        component: my,
        meta:{
            title:'我的 - 饿了么',
            requiresAuth:false
        }
    }

]

// 创建路由实例
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    // 设置页面标题
    if (to.meta.title) {
        document.title = to.meta.title as string
    } else {
        document.title = '饿了么'
    }

    // 检查路由是否需要认证
    if (to.meta.requiresAuth) {
        const isAuthenticated = checkUserAuthentication()

        if (!isAuthenticated) {
            // 如果未登录，重定向到登录页
            next({ name: 'Login', query: { redirect: to.fullPath } })
            return
        }
    }

    next()
})

// 认证检查函数
function checkUserAuthentication(): boolean {
    // 实际项目中检查本地存储的token或cookie
    return localStorage.getItem('authToken') !== null
}

export default router
